/*
 * Copyright (C) 2015 hu
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.sentiment.webcollector.crawler;

import com.sentiment.webcollector.extract.Extractor;
import com.sentiment.webcollector.extract.ExtractorParams;
import com.sentiment.webcollector.extract.Extractors;
import com.sentiment.webcollector.extract.RegexExtractorFactory;
import com.sentiment.webcollector.model.Links;
import com.sentiment.webcollector.model.Page;

/**
 *
 * @author hu
 */
public class MultiExtractorCrawler extends BreadthCrawler {

	protected RegexExtractorFactory regexExtractorFactory = new RegexExtractorFactory();

	public MultiExtractorCrawler(boolean autoParse) {
		super(autoParse);
	}

	@Override
	public void visit(Page page, Links nextLinks) {
		try {
			Extractors extractors = regexExtractorFactory.createExtractor(page);
			for (Extractor extractor : extractors) {
				try {
					extractor.execute(nextLinks);
				} catch (Exception ex) {
					LOG.info("Exception", ex);
				}
			}
		} catch (Exception ex) {
			LOG.info("Exception", ex);
		}
	}

	public void addExtractor(String urlRegex, Class<? extends Extractor> extractorClass) {
		regexExtractorFactory.addExtractor(urlRegex, extractorClass);
	}

	public void addExtractor(String urlRegex, Class<? extends Extractor> extractorClass, ExtractorParams params) {
		regexExtractorFactory.addExtractor(urlRegex, extractorClass, params);
	}

	public RegexExtractorFactory getRegexExtractorFactory() {
		return regexExtractorFactory;
	}

	public void setRegexExtractorFactory(RegexExtractorFactory regexExtractorFactory) {
		this.regexExtractorFactory = regexExtractorFactory;
	}

}
